package tw.twandroidproj.app.ui.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import com.google.gson.reflect.TypeToken;
import tw.twandroidproj.app.R;
import tw.twandroidproj.app.model.ErrorModel;
import tw.twandroidproj.app.model.Job;
import tw.twandroidproj.app.network.NetParams;
import tw.twandroidproj.app.network.NetworkManager;
import tw.twandroidproj.app.network.OnResponseListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Frezc on 2015/11/8.
 */
public class NetTestActivity extends AppCompatActivity implements OnResponseListener<List<Job>>{

    private AppCompatEditText query;
    private TextView tv;
    private NetworkManager networkManager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nettest);
        setSupportActionBar((Toolbar) findViewById(R.id.nettest_toolbar));
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        networkManager = NetworkManager.getInstance(this);

        query = (AppCompatEditText) findViewById(R.id.nettest_query);
        tv = (TextView) findViewById(R.id.nettest_result);
        findViewById(R.id.nettest_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TestNetwork();
            }
        });

    }

    private void TestNetwork(){

        //测试搜索岗位
        Map<String, String> query = new HashMap<String, String>();
        query.put("q", this.query.getEditableText().toString());
        query.put("limit", "10");
        networkManager.get(NetParams.structureQueryUrl(NetParams.JOB_QUERY_URL, query), this, new TypeToken<List<Job>>(){}.getType());


        /*
        //得到一个岗位
        networkManager.get(NetParams.structurePathUrl(NetParams.JOB_URL, "0"), new OnResponseListener<Job>() {
            @Override
            public void onSuccess(Job result) {
                tv.setText(result.toString());
            }

            @Override
            public void onFailed(ErrorModel errorModel) {

            }
        }, Job.class);
        */


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public void onSuccess(List<Job> result) {

        StringBuilder sb = new StringBuilder();
        for(Job job : result){
            sb.append(job.toString());
            sb.append("\n");
        }


        tv.setText(sb.toString());
    }

    @Override
    public void onFailed(ErrorModel errorModel) {

    }
}
